package com.mapproject.industrymap.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapproject.industrymap.service.SysAutoPublishService;
import com.mapproject.industrymap.utils.ReturnValue;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.decoder.RESTLayerGroup;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.GSLayerGroupEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.List;
import java.util.Map;

@Service
public class SysAutoPublishServiceImpl implements SysAutoPublishService {
    @Value("${file.uploadFolder}")
    private String uploadFolder;
    @Value("${file.projectFileDir}")
    private String projectFileDir;
    public ReturnValue autoPublish(String layername){
        //geoserver连接配置
        String RESTURL  = "http://localhost:8080/geoserver";
        String RESTUSER = "admin";
        String RESTPW   = "geoserver";
        //postGIS连接配置
        String postGisHost = "http://localhost";
        int postGisPort = 5432;
        String postGisUser = "postgres";
        String postGisPassWord = "lvshuangxi";
        String postGisDatabase = "WJMap";

        //List<String> workspaces=null;
        String result="";
        try {
            //File zipFile=new File("E:\\programs\\geoserverTest\\geoserverManager\\src\\main\\resources\\testdata\\shapefile\\cities.zip");
            //服务参数
            String ws = "WJMap";
            String store_name = "WJMap";
            String table_name = layername.toLowerCase();
            //String table_name = "TDYTQ2";
            //判断workspace是否存在，若不存在则创建
            URL u = new URL(RESTURL);
            GeoServerRESTManager manager = new GeoServerRESTManager(u, RESTUSER, RESTPW);
            GeoServerRESTPublisher publisher = manager.getPublisher();
            List<String> workspaces = manager.getReader().getWorkspaceNames();
            if (!workspaces.contains(ws))
            {
                boolean creatws = publisher.createWorkspace(ws);
                System.out.println("create ws:"+creatws);
            }else
            {
                System.out.println("workspace已经存在:"+ws);
            }

            //判断datastore是否存在，若不存在则创建
            RESTDataStore restDataStore = manager.getReader().getDatastore(ws,store_name);
            if (restDataStore == null)
            {
                GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(store_name);
                store.setHost(postGisHost);
                store.setPort(postGisPort);
                store.setUser(postGisUser);
                store.setPassword(postGisPassWord);
                store.setDatabase(postGisDatabase);
                store.setSchema("public");
                store.setConnectionTimeout(20);
                store.setMaxConnections(20);
                store.setMinConnections(1);
                store.setExposePrimaryKeys(true);
                boolean creatStore = manager.getStoreManager().create(ws,store);
                System.out.println("create datastore:"+creatStore);
            }else
            {
                System.out.println("数据存储已经存在:"+ store_name);
            }

            //判断layer是否存在，若不存在则创建
            RESTLayer layer = manager.getReader().getLayer(ws,table_name);
            if (layer == null)
            {
                GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
                pds.setTitle(table_name);
                pds.setName(table_name);
                pds.setSRS("EPSG:4528");


                GSLayerEncoder layerEncoder = new GSLayerEncoder();
                //layerEncoder.setDefaultStyle("土地用途区");

                boolean publish = manager.getPublisher().publishDBLayer(ws,store_name,pds,layerEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ table_name);
            }

//            GeoServerRESTReader reader = new GeoServerRESTReader(RESTURL, RESTUSER, RESTPW);
//            GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(RESTURL, RESTUSER, RESTPW);


            //发布图层
            /*if(workspaces.contains("ce")){
                if(publisher.publishShp("ce", "resttestshp", "cities", zipFile, "EPSG:4326")){
                    result="He did it!";
                }else{
                    result="fail!";
                }
            }*/
            //获取style列表
            /*RESTStyleList ls = reader.getStyles();
            for(int i=0; i<ls.size(); i++){
                System.out.println(ls.get(i).getName());
            }*/



        }catch (Exception mue){
            mue.printStackTrace();
        }







        return new ReturnValue(200,"图层发布成功");
    }

    public ReturnValue autoPublishDxf(String layername){
        //geoserver连接配置
        String RESTURL  = "http://localhost:8080/geoserver";
        String RESTUSER = "admin";
        String RESTPW   = "geoserver";
        //postGIS连接配置
        String postGisHost = "http://localhost";
        int postGisPort = 5432;
        String postGisUser = "postgres";
        String postGisPassWord = "lvshuangxi";
        String postGisDatabase = "WJMap";

        //List<String> workspaces=null;
        String result="";
        try {
            //服务参数
            String ws = "WJMap";
            String store_name = "WJMap";
            String layer_name = layername.toLowerCase();
            String group_name = layer_name;
            String table_name_PG = layer_name+"_pg";
            String table_name_PL = layer_name+"_pl";
            String table_name_PT = layer_name+"_pt";
            //判断workspace是否存在，若不存在则创建
            URL u = new URL(RESTURL);
            GeoServerRESTManager manager = new GeoServerRESTManager(u, RESTUSER, RESTPW);
            GeoServerRESTPublisher publisher = manager.getPublisher();
            List<String> workspaces = manager.getReader().getWorkspaceNames();
            if (!workspaces.contains(ws))
            {
                boolean creatws = publisher.createWorkspace(ws);
                System.out.println("create ws:"+creatws);
            }else
            {
                System.out.println("workspace已经存在:"+ws);
            }

            //判断datastore是否存在，若不存在则创建
            RESTDataStore restDataStore = manager.getReader().getDatastore(ws,store_name);
            if (restDataStore == null)
            {
                GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(store_name);
                store.setHost(postGisHost);
                store.setPort(postGisPort);
                store.setUser(postGisUser);
                store.setPassword(postGisPassWord);
                store.setDatabase(postGisDatabase);
                store.setSchema("public");
                store.setConnectionTimeout(20);
                store.setMaxConnections(20);
                store.setMinConnections(1);
                store.setExposePrimaryKeys(true);
                boolean creatStore = manager.getStoreManager().create(ws,store);
                System.out.println("create datastore:"+creatStore);
            }else
            {
                System.out.println("数据存储已经存在:"+ store_name);
            }

            //判断三个layer是否存在，若不存在则创建
            RESTLayer layer_PG = manager.getReader().getLayer(ws,table_name_PG);
            if (layer_PG == null)
            {
                GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
                pds.setTitle(table_name_PG);
                pds.setName(table_name_PG);
                pds.setSRS("EPSG:4528");


                GSLayerEncoder layerEncoder = new GSLayerEncoder();
                layerEncoder.setDefaultStyle("testdxf_pg");

                boolean publish = manager.getPublisher().publishDBLayer(ws,store_name,pds,layerEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ table_name_PG);
            }
            RESTLayer layer_PL = manager.getReader().getLayer(ws,table_name_PL);
            if (layer_PL == null)
            {
                GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
                pds.setTitle(table_name_PL);
                pds.setName(table_name_PL);
                pds.setSRS("EPSG:4528");


                GSLayerEncoder layerEncoder = new GSLayerEncoder();
                layerEncoder.setDefaultStyle("testdxf_pl");

                boolean publish = manager.getPublisher().publishDBLayer(ws,store_name,pds,layerEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ table_name_PL);
            }
            RESTLayer layer_PT = manager.getReader().getLayer(ws,table_name_PT);
            if (layer_PT == null)
            {
                GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
                pds.setTitle(table_name_PT);
                pds.setName(table_name_PT);
                pds.setSRS("EPSG:4528");


                GSLayerEncoder layerEncoder = new GSLayerEncoder();
                layerEncoder.setDefaultStyle("testdxf_pt");

                boolean publish = manager.getPublisher().publishDBLayer(ws,store_name,pds,layerEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ table_name_PT);
            }

            //生成图层组
            RESTLayerGroup layerGroup = manager.getReader().getLayerGroup(ws,group_name);
            if (layerGroup == null)
            {

                GSLayerGroupEncoder layerGroupEncoder = new GSLayerGroupEncoder();
                //layerGroupEncoder.setWorkspace(ws);
                //layerGroupEncoder.setName(group_name);
                String layer_name_pg="WJMap:"+table_name_PG;
                String layer_name_pl="WJMap:"+table_name_PL;
                String layer_name_pt="WJMap:"+table_name_PT;
                layerGroupEncoder.addLayer(layer_name_pg);
                layerGroupEncoder.addLayer(layer_name_pl);
                layerGroupEncoder.addLayer(layer_name_pt);
                boolean publish = manager.getPublisher().createLayerGroup(ws,group_name,layerGroupEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ group_name);
            }


        }catch (Exception mue){
            mue.printStackTrace();
        }


        return new ReturnValue(200,"图层发布成功");
    }

    public ReturnValue autoPublishGeojson(String layername){
        //geoserver连接配置
        String RESTURL  = "http://localhost:8080/geoserver";
        String RESTUSER = "admin";
        String RESTPW   = "geoserver";
        //postGIS连接配置
        String postGisHost = "http://localhost";
        int postGisPort = 5432;
        String postGisUser = "postgres";
        String postGisPassWord = "lvshuangxi";
        String postGisDatabase = "WJMap";

        //List<String> workspaces=null;
        String result="";
        try {
            //服务参数
            String ws = "WJMap";
            String store_name = "WJMap";
            String layer_name = layername.toLowerCase();
            String group_name = layer_name;
            String table_name_PG = layer_name+"_pg";
            String table_name_PL = layer_name+"_pl";
            String table_name_PT = layer_name+"_pt";
            //判断workspace是否存在，若不存在则创建
            URL u = new URL(RESTURL);
            GeoServerRESTManager manager = new GeoServerRESTManager(u, RESTUSER, RESTPW);
            GeoServerRESTPublisher publisher = manager.getPublisher();
            List<String> workspaces = manager.getReader().getWorkspaceNames();
            if (!workspaces.contains(ws))
            {
                boolean creatws = publisher.createWorkspace(ws);
                System.out.println("create ws:"+creatws);
            }else
            {
                System.out.println("workspace已经存在:"+ws);
            }

            //判断datastore是否存在，若不存在则创建
            RESTDataStore restDataStore = manager.getReader().getDatastore(ws,store_name);
            if (restDataStore == null)
            {
                GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(store_name);
                store.setHost(postGisHost);
                store.setPort(postGisPort);
                store.setUser(postGisUser);
                store.setPassword(postGisPassWord);
                store.setDatabase(postGisDatabase);
                store.setSchema("public");
                store.setConnectionTimeout(20);
                store.setMaxConnections(20);
                store.setMinConnections(1);
                store.setExposePrimaryKeys(true);
                boolean creatStore = manager.getStoreManager().create(ws,store);
                System.out.println("create datastore:"+creatStore);
            }else
            {
                System.out.println("数据存储已经存在:"+ store_name);
            }

            //判断三个layer是否存在，若不存在则创建
            RESTLayer layer_PG = manager.getReader().getLayer(ws,table_name_PG);
            if (layer_PG == null)
            {
                GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
                pds.setTitle(table_name_PG);
                pds.setName(table_name_PG);
                pds.setSRS("EPSG:4528");


                GSLayerEncoder layerEncoder = new GSLayerEncoder();
                //layerEncoder.setDefaultStyle("testdxf_pg");

                boolean publish = manager.getPublisher().publishDBLayer(ws,store_name,pds,layerEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ table_name_PG);
            }
            RESTLayer layer_PL = manager.getReader().getLayer(ws,table_name_PL);
            if (layer_PL == null)
            {
                GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
                pds.setTitle(table_name_PL);
                pds.setName(table_name_PL);
                pds.setSRS("EPSG:4528");


                GSLayerEncoder layerEncoder = new GSLayerEncoder();
                //layerEncoder.setDefaultStyle("testdxf_pl");

                boolean publish = manager.getPublisher().publishDBLayer(ws,store_name,pds,layerEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ table_name_PL);
            }
            RESTLayer layer_PT = manager.getReader().getLayer(ws,table_name_PT);
            if (layer_PT == null)
            {
                GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
                pds.setTitle(table_name_PT);
                pds.setName(table_name_PT);
                pds.setSRS("EPSG:4528");


                GSLayerEncoder layerEncoder = new GSLayerEncoder();
                //layerEncoder.setDefaultStyle("testdxf_pt");

                boolean publish = manager.getPublisher().publishDBLayer(ws,store_name,pds,layerEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ table_name_PT);
            }

            //生成图层组
            RESTLayerGroup layerGroup = manager.getReader().getLayerGroup(ws,group_name);
            if (layerGroup == null)
            {

                GSLayerGroupEncoder layerGroupEncoder = new GSLayerGroupEncoder();
                //layerGroupEncoder.setWorkspace(ws);
                //layerGroupEncoder.setName(group_name);
                String layer_name_pg="WJMap:"+table_name_PG;
                String layer_name_pl="WJMap:"+table_name_PL;
                String layer_name_pt="WJMap:"+table_name_PT;
                layerGroupEncoder.addLayer(layer_name_pg);
                layerGroupEncoder.addLayer(layer_name_pl);
                layerGroupEncoder.addLayer(layer_name_pt);
                boolean publish = manager.getPublisher().createLayerGroup(ws,group_name,layerGroupEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ group_name);
            }


        }catch (Exception mue){
            mue.printStackTrace();
        }


        return new ReturnValue(200,"图层发布成功");
    }
}

